/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import data.Departments;
import dao.DepartmentDAO;
import data.Sections;
import dao.SectionDAO;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.TableModel;

/**
 *
 * @author ChiHieu
 */
public class SectionBO {

    private DepartmentDAO dDAO = new DepartmentDAO();
    private SectionDAO sDAO = new SectionDAO();
    private Sections secData = new Sections();
    boolean error = false;
    private String information = "";

    public String getInformation() {
        return information;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public boolean Add(String depName, String sectionId, String sectionName, String sectionInch) {
        Departments dptemp = new Departments();
        try {
            dptemp = dDAO.getDepartmentByTagName(depName);
        } catch (Exception ee) {
            information += "You forgot to fill in the DEPNAME!\n";
        }
        for (Sections sec : sDAO.getAllSection()) {
            if (sec.getSectid().equals(sectionId)) {
                information += "SECTIONID is alredy exist!\n";
                error = true;
                break;
            }
        }
        if (sectionId.length() == 0) {
            information += "You forgot to fill in the SECID!\n";
            error = true;
        }
        if (sectionName.length() == 0) {
            information += "You forgot to fill in the SECNAME!\n";
            error = true;
        }
        if (sectionInch.length() == 0) {
            information += "You forgot to fill in the SECINCH!\n";
            error = true;
        }
        if (!error) {
            //Departments dptemp = new Departments();
//			for (Departments dm : dDAO.getAll()) {
//				if (dm.getDeptname().equals(depName)) {
//					dptemp = dm;
//					break;
//				}
//			}
            secData.setDepartments(dptemp);
            secData.setSectid(sectionId);
            secData.setSectname(sectionName);
            secData.setSectioninch(sectionInch);

            if (sDAO.executeInsertHQLQuery(secData)) {
                information = "Insert your data has been successfully!";
            } else {
                information = "Insert your data has been fail!";
            }

        }
        return error;
    }
    public TableModel actionShowTableSection() {
        return sDAO.showTableSection();
    }

    public void actionUpdate(Object selectedItem, String secID_tmp, String secname, String secinch) {
        //Departments dptemp = new Departments();
        if (selectedItem.toString().length() == 0) {
            error = true;
            information += "You forgot to fill in the DEPNAME!\n";
        }
        if (secname.length() == 0) {
            error = true;
            information += "You forgot to fill in the SECTIONNAME!\n";
        }
        if (secinch.length() == 0) {
            error = true;
            information += "You forgot to fill in the SECTIONINCH!\n";
        }
        if (!error) {
            Sections sectemp = new Sections(secID_tmp);
            for (Departments dp : dDAO.getAll()) {
                if (dp.getDeptname().equals(selectedItem)) {
                    //dptemp = dp;
                    sectemp.setDepartments(dp);
                    break;
                }
            }
            sectemp.setSectname(secname);
            sectemp.setSectioninch(secinch);
            sDAO.executeUpdateHQLQuery(sectemp);
            information = "Update your data has been successfully!";
        }
    }

    public boolean actionDelete(Sections sections) {
        if (sDAO.executeDeleteHQLQuery(sections)) {
            information = "Deleteting has been successfully!";
            return true;
        } else {
            information = "Could not Delete!";
            return false;
        }
    }

    public ComboBoxModel getComboBoxModel() {
       return dDAO.getComboBoxModel();
    }
    //hung

    public List<Sections> showAllSections(Object tmpdeptname) {
        if (tmpdeptname != null) {
            String deptname = (String) tmpdeptname;
            List<Sections> lSec = new dao.SectionDAO().getAllSections(deptname);
            return lSec;
        } else {
            throw new Error("Please,check your information");
        }
    }
}
